package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询、git branch -m <old_name> <new_name>
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        setmealService.page(pageInfo,wrapper);

        //赋值给Dto
        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<Setmeal> pageInfoRecords = pageInfo.getRecords();

        List<SetmealDto> records = new ArrayList<>();
        //把Setmeal的值 赋值给Dto 里的父类空间
        for (Setmeal setmeal : pageInfoRecords) {
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(setmeal,setmealDto);

            //获取dto里面的 套餐名称
            Category category = categoryService.getById(setmeal.getCategoryId());

            setmealDto.setCategoryName(category.getName());

            records.add(setmealDto);
        }

        setmealDtoPage.setRecords(records);

        return R.success(setmealDtoPage);
    }

    /**
     * 添加套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);

        return R.success("添加套餐成功");
    }

    /**
     * 批量添加
     */

    @PostMapping("/status/{ss}")
    public R<String> status(@PathVariable Integer ss, Long[] ids){
        for (Long id : ids) {
            LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();

            wrapper.set(Setmeal::getStatus,ss);
            wrapper.eq(Setmeal::getId,id);

            setmealService.update(wrapper);

        }
        return R.success("状态修改成功");
    }


    /**
     * 删除
     */
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        for (Long id : ids) {
            //先删除 此套餐下的菜品
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,id);

            List<SetmealDish> dishes = setmealDishService.list(wrapper);
            for (SetmealDish dish : dishes) {
                setmealDishService.removeById(dish.getId());
            }

            setmealService.removeById(id);
        }

        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List> list(Setmeal setmeal){// categoryId 和 状态
        log.info("需要查询的套餐分类id是:"+setmeal.getCategoryId());
        // select *from setmeal where categort_id=? and status=1
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());// categort_id=?
        wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        //  如果传递了状态 就按照状态去查询
        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }
}